package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.AdSensitiveService;
import com.usian.common.exception.CatchCustomException;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.utils.threadlocal.AdminThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;


@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    @Autowired
    AdSensitiveMapper adSensitiveMapper;

    @Override
    public ResponseResult findPage(SensitiveDto dto) {

        // 只用是admin用户才能使用这个方法（只要登录用户才能访问）
        // 1. 解析token  2. 获得用户Id  根据用户Id 查询用户信息
        // 过滤器
//        AdUser adUser =  AdminThreadLocalUtils.getUser();
//        System.out.println(adUser.getId());

        //1 先判断 dto 是否为空
        if (dto==null){
            CatchCustomException.catchs(20007,"参数不合法");
        }
        // 2 初始化分页参数
           dto.checkParam();
        // 3 设置查询条件
        LambdaQueryWrapper<AdSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(dto.getName()!=null&&!"".equals(dto.getName()),AdSensitive::getSensitives,dto.getName());
        // 4， 设置分页条件
        Page<AdSensitive> page= new Page<>(dto.getPage(),dto.getSize());
        //5 执行查询
        IPage<AdSensitive> iPage = this.page(page, lambdaQueryWrapper);
        //  封装返回结果
        PageResponseResult pageResponseResult = new PageResponseResult(iPage.getCurrent(),iPage.getSize(),iPage.getTotal());
        pageResponseResult.setData(iPage.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult saveAdSensitive(AdSensitive adSensitive) {
        // 1，判断 是否为空
           if (adSensitive==null){
               CatchCustomException.catchs(20007,"参数不合法");
           }
//        //2. 维护初始化字段
//        adSensitive.setCreatedTime(new Date());
//        // 3. 添加

        // 维护初始化字段
        if (adSensitive.getCreatedTime()==null){
            adSensitive.setCreatedTime(new Date());
        }
        this.save(adSensitive);
        return ResponseResult.okResult(null);
    }

    @Override
    public ResponseResult del(Integer id) {
        // 1 判断
        if(id==null){
            CatchCustomException.catchs(20008,"ID不能为空");
        }
        //2 删除
        this.removeById(id);
        return ResponseResult.okResult(null);
    }

    @Override
    public ResponseResult dels(Integer[] ids) {
        // 1 判断
        if (ids==null || ids.length<1){
            CatchCustomException.catchs(20008,"ID不能为空");
        }
        //2 删除
        this.removeByIds(Arrays.asList(ids));
        return ResponseResult.okResult(null);
    }


    @Override
    public ResponseResult dels2(Integer[] ids) {
        // 1 判断
        if (ids==null || ids.length<1){
            CatchCustomException.catchs(20008,"ID不能为空");
        }
        //2 删除
          adSensitiveMapper.delete2(ids);
        return ResponseResult.okResult(null);
    }


    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        // 1 判断 参数是否 为空
        if (adSensitive==null){
            CatchCustomException.catchs(20007,"参数不合法");
        }


        // 2  修改
         adSensitive.setId(adSensitive.getId());
         this.updateById(adSensitive);
        return ResponseResult.okResult(null);
    }

    @Override
    public ResponseResult findById(Integer id) {
        // 1 判断 id
        if (id==null){
            CatchCustomException.catchs(20008,"ID不能为空");
        }
        AdSensitive adSensitive = this.getById(id);
        return ResponseResult.okResult(adSensitive);
    }
}
